using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Nuclex.Game.States;
using Nuclex.UserInterface;
using Nuclex.Input;

namespace WarPigs
{
    [Serializable]
    public struct pmcData
    {
        public string pmcName;
        public string pmcColor;
        public Color pmcColorC;

        //To support multiple local pmcs, enable below and make all variables arrays
        //Call to serialize with pmcData = new pmcData(#) where # is the number of PMCs to add
        //
        //public int Count;

        //public pmcData(int count)
        //{
        //    pmcName = new string[count];
        //    pmcColor = new int[count];

        //    Count = count;
        //}
    }


    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class WarPigs : Microsoft.Xna.Framework.Game
    {
        protected GraphicsDeviceManager graphics;
        
        SpriteBatch spriteBatch; //Not sure if I need this...

        //Game States
        GameStateManager gameStateManager;
        SPStartingGameState spStartingGameState;
        MainMenuGameState mainMenuGameState;
        InGameGameState inGameGameState;
        OpeningScreenGameState openingScreenGameState;
        

        //UI Management
        private GuiManager guiManager;
        InputManager inputManager;
        
        //Data Management
        public pmcData data;
        public LocalPlayerData localPlayerData;

        //Difficulty Management
        public Difficulty difficulty;
     
        public WarPigs()
        {
            graphics = new GraphicsDeviceManager(this)
            {
                //PreferredBackBufferWidth = 1024,
                //PreferredBackBufferHeight = 768,
                PreferredBackBufferWidth = 1024,
                PreferredBackBufferHeight = 768,
                IsFullScreen = false,
                //PreferMultiSampling = false
            };

            Content.RootDirectory = "Content";

            gameStateManager = new GameStateManager(this.Services);
            guiManager = new GuiManager(this.Services);
            inputManager = new InputManager(this.Services);

            Components.Add(gameStateManager);
            Components.Add(guiManager);
            Components.Add(inputManager);

            




        }
        
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.IsMouseVisible = true;


            //GameState
            gameStateManager.Initialize();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            //Pulls difficulty variables
            difficulty = new Difficulty("Beginner");

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(this.GraphicsDevice);

            Viewport viewport = GraphicsDevice.Viewport;

            //Gamestate Management - Loads the states and prepares them.
            //gameStateManager.DisposeDroppedStates = true;
            spStartingGameState = new SPStartingGameState(graphics, this.GraphicsDevice, this.Content, spriteBatch, guiManager, inputManager, gameStateManager);
            inGameGameState = new InGameGameState(graphics, this.GraphicsDevice, this.Content, spriteBatch, guiManager, this);
            openingScreenGameState = new OpeningScreenGameState(graphics, this.GraphicsDevice, this.Content, spriteBatch, guiManager, inputManager, gameStateManager, this);
            mainMenuGameState = new MainMenuGameState(graphics, this.GraphicsDevice, this.Content, spriteBatch, guiManager, inputManager, gameStateManager, this, data);            
            
            
            
            //FPS Tracker
            //dgcFPS FPSComponent = new dgcFPS(this);
            //this.Components.Add(FPSComponent);
            //FPSComponent.ShowFPS = true;
            gameStateManager.Push(openingScreenGameState);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            Window.Title = mainMenuGameState.test;
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);


            // TODO: Add your drawing code here
            //(hilightPoint.X * Tile.TileStepX) + hilightrowOffset,

                                    //(hilightPoint.Y) * Tile.TileStepY)
            base.Draw(gameTime);
        }

        public void ChangeState(string State)
        {
            gameStateManager.Pop();
            switch (State)
            {
                case "inGameGameState": gameStateManager.Push(inGameGameState); break;
                case "mainMenuGameState": gameStateManager.Push(mainMenuGameState); break;
            }
        }
    }
}
